const app = getApp()
Page({
  onLoad() {
    wx.cloud.init()
    this.ctx = wx.createCameraContext()
    wx.cloud.callFunction({
      name: 'login',
      data: {},
      success: res => {
        this.openid = res.result.openid
      },
      fail: err => {
        console.error(err)
      }
    })
    // this.openid = app.globalData.userInfo.openid
    // console.log(this.openid)
  },
  onTapVerify() {
    var tempInagePath
    var fileID
    this.ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        wx.showLoading({
          title: '验证中...',
          mask: true
        })
        // wx.getFileSystemManager().readFileSync(res.tempFilePaths[0], "base64")
        this.tempImagePath = res.tempImagePath
        var imgstring = wx.getFileSystemManager().readFileSync(this.tempImagePath, "base64")
        console.log(imgstring)
        wx.cloud.callFunction({
          name: 'Get_QRCode',
          data: {
              groupid: "public",
              personid: this.openid,
              img: imgstring
          },
          success: res => {
            console.log("===============")
            console.log(res.result.result)
            if(res.result.result){
              wx.hideLoading()
              wx.showToast({
                title: '成功',
                icon: 'success',
                duration: 1000
              })
              console.log(res.result.md5string)
              wx.navigateTo({
                url: '../qrcode/qrcode?md5string='+res.result.md5string,
              })
            }else{
              wx.hideLoading()
              wx.showToast({
                title: '失败,请重试',
                icon: 'none',
                duration: 1000
              })
            }

          },
          fail: err => {
            wx.hideLoading()
            wx.showToast({
              title: '失败',
              icon: 'none',
              duration: 1000
            })
          }
        })
      }
    })
  },
  error(e) {
    console.log(e.detail)
  }
})